<html>
<body>

<?php include 'mainheader.php'; ?>
<?php include 'databaseutil.php'; ?>


<?php

// Main

if( !isset($_GET["month"]) || !isset($_GET["year"]) )
{
	echo "<br><br>";
	echo "<form name='input' action='compare.php' method='get'>";

	echo "Month: "; //<input type='text' name='month'><br>";
	echo "<select name='month'>";
		echo "<option value='1'>January</option>";
		echo "<option value='2'>February</option>";
		echo "<option value='3'>March</option>";
		echo "<option value='4'>April</option>";
		echo "<option value='5'>May</option>";
		echo "<option value='6'>June</option>";
		echo "<option value='7'>July</option>";
		echo "<option value='8'>August</option>";
		echo "<option value='9'>September</option>";
		echo "<option value='10'>October</option>";
		echo "<option value='11'>November</option>";
		echo "<option value='12'>December</option>";
	echo "</select>";
	echo "<br>";
	echo "Year: <input type='text' name='year'><br>";
	echo "<br>";
	echo "<input type='submit' value='Submit'>";
	echo "</form>";
	return;
}

// Get values from form
$month = $_GET["month"];
$year = $_GET["year"];

// Get asset values according to date
$cg = getAssetValue( "2Qtos C. Grande", $month, $year );
$cu = getAssetValue( "2Qtos Curicica", $month, $year );
$g = getAssetValue( "GOLD", $month, $year );
$s = getAssetValue( "SILVER", $month, $year );
$do = getAssetValue( "DOW", $month, $year );
$b = getAssetValue( "iBovespa", $month, $year );
$p = getAssetValue( "PETR4", $month, $year );
$v = getAssetValue( "VALE5", $month, $year );
$o = getAssetValue( "OGXP3", $month, $year );
$u = getAssetValue( "USIM5", $month, $year );
$dr = getAssetValue( "Dollar", $month, $year );
$er = getAssetValue( "Euro", $month, $year );

// Valida as informacoes do banco para evitar divisao por zero
if( $cg == NULL || $cu == NULL || $g == NULL || $s == NULL || $do == NULL || $b == NULL || $p == NULL || $v == NULL || $o == NULL || $u == NULL || $dr == NULL || $er == NULL )
{
	echo "<br><br>";
	echo "<h4>Sorry, this month/year doest not exist in database.</h4>";
	echo "<a href = 'compare.php'>Back</a>";
	return;
}

if( $cg == 0 || $cu == 0 || $g == 0 || $s == 0 || $do == 0 || $b == 0 || $p == 0 || $v == 0 || $o == 0 || $u == 0 || $dr == 0 || $er == 0 )
{
	echo "<br><br>";
	echo "<h4>Error. At least one of these assets is about to evoke a division by zero error. Check it out!!</h4>";
	echo "<h5>2Qtos Campo Grande: " . $cg . "</h5>";
	echo "<h5>2Qtos Curicica: " . $cu . "</h5>";
	echo "<h5>Gold: " . $g . "</h5>";
	echo "<h5>Silver: : " . $s . "</h5>";
	echo "<h5>Dow: " . $do . "</h5>";
	echo "<h5>iBovespa: " . $b . "</h5>";
	echo "<h5>PETR4: " . $p . "</h5>";
	echo "<h5>VALE5: " . $v . "</h5>";
	echo "<h5>OGXP3: " . $o . "</h5>";
	echo "<h5>USIM5: " . $u . "</h5>";
	echo "<h5>Dollar: " . $dr . "</h5>";
	echo "<h5>Euro: " . $er . "</h5>";
	echo "<a href = 'compare.php'>Back</a>";
	return;
}

// Save values read from database
$a[1][1]  = $cg;
$a[2][1]  = $cu;
$a[3][1]  = $g;
$a[4][1]  = $s;
$a[5][1]  = $do;
$a[6][1]  = $b;
$a[7][1]  = $p;
$a[8][1]  = $v;
$a[9][1]  = $o;
$a[10][1] = $u;
$a[11][1] = $dr;
$a[12][1] = $er;

// Do the math

// 2Qtos Campo Grande
$a[1][2]  = 1;
$a[2][2]  = number_format($cu/$cg, 6);
$a[3][2]  = number_format($g/$cg, 6);
$a[4][2]  = number_format($s/$cg, 6);
$a[5][2]  = number_format($do/$cg, 6);;
$a[6][2]  = number_format($b/$cg, 6);;
$a[7][2]  = number_format($p/$cg, 6);;
$a[8][2]  = number_format($v/$cg, 6);;
$a[9][2]  = number_format($o/$cg, 6);;
$a[10][2] = number_format($u/$cg, 6);;
$a[11][2] = number_format($dr/$cg, 6);;
$a[12][2] = number_format($er/$cg, 6);;

// 2Qtos Curicica
$a[1][3]  = number_format($cg/$cu, 6);
$a[2][3]  = 1;
$a[3][3]  = number_format($g/$cu, 6);
$a[4][3]  = number_format($s/$cu, 6);
$a[5][3]  = number_format($do/$cu, 6); 
$a[6][3]  = number_format($b/$cu, 6);  
$a[7][3]  = number_format($p/$cu, 6);  
$a[8][3]  = number_format($v/$cu, 6);  
$a[9][3]  = number_format($o/$cu, 6);  
$a[10][3] = number_format($u/$cu, 6);  
$a[11][3] = number_format($dr/$cu, 6); 
$a[12][3] = number_format($er/$cu, 6); 

// Gold
$a[1][4]  = number_format($cg/$g, 6);  
$a[2][4]  = number_format($cu/$g, 6); 
$a[3][4]  = 1;      
$a[4][4]  = number_format($s/$g, 6);  
$a[5][4]  = number_format($do/$g, 6); 
$a[6][4]  = number_format($b/$g, 6);  
$a[7][4]  = number_format($p/$g, 6);  
$a[8][4]  = number_format($v/$g, 6);  
$a[9][4]  = number_format($o/$g, 6);  
$a[10][4] = number_format($u/$g, 6);  
$a[11][4] = number_format($dr/$g, 6);  
$a[12][4] = number_format($er/$g, 6);  

// Silver
$a[1][5]  = number_format($cg/$s, 6); 
$a[2][5]  = number_format($cu/$s, 6); 
$a[3][5]  = number_format($g/$s, 6);  
$a[4][5]  = 1;      
$a[5][5]  = number_format($do/$s, 6); 
$a[6][5]  = number_format($b/$s, 6);  
$a[7][5]  = number_format($p/$s, 6);  
$a[8][5]  = number_format($v/$s, 6);  
$a[9][5]  = number_format($o/$s, 6);  
$a[10][5] = number_format($u/$s, 6);  
$a[11][5] = number_format($dr/$s, 6); 
$a[12][5] = number_format($er/$s, 6); 

// DOW
$a[1][6]  = number_format($cg/$do, 6); 
$a[2][6]  = number_format($cu/$do, 6); 
$a[3][6]  = number_format($g/$do, 6);  
$a[4][6]  = number_format($s/$do, 6);  
$a[5][6]  = 1;       
$a[6][6]  = number_format($b/$do, 6);  
$a[7][6]  = number_format($p/$do, 6);  
$a[8][6]  = number_format($v/$do, 6);  
$a[9][6]  = number_format($o/$do, 6);  
$a[10][6] = number_format($u/$do, 6);  
$a[11][6] = number_format($dr/$do, 6); 
$a[12][6] = number_format($er/$do, 6); 

// Bovespa
$a[1][7]  = number_format($cg/$b, 6); 
$a[2][7]  = number_format($cu/$b, 6); 
$a[3][7]  = number_format($g/$b, 6);  
$a[4][7]  = number_format($s/$b, 6);  
$a[5][7]  = number_format($do/$b, 6); 
$a[6][7]  = 1;      
$a[7][7]  = number_format($p/$b, 6);  
$a[8][7]  = number_format($v/$b, 6);  
$a[9][7]  = number_format($o/$b, 6);  
$a[10][7] = number_format($u/$b, 6);  
$a[11][7] = number_format($dr/$b, 6); 
$a[12][7] = number_format($er/$b, 6); 

// PETR4
$a[1][8]  = number_format($cg/$p, 6); 
$a[2][8]  = number_format($cu/$p, 6); 
$a[3][8]  = number_format($g/$p, 6);  
$a[4][8]  = number_format($s/$p, 6);  
$a[5][8]  = number_format($do/$p, 6); 
$a[6][8]  = number_format($b/$p, 6);  
$a[7][8]  = 1;      
$a[8][8]  = number_format($v/$p, 6);  
$a[9][8]  = number_format($o/$p, 6);  
$a[10][8] = number_format($u/$p, 6);  
$a[11][8] = number_format($dr/$p, 6); 
$a[12][8] = number_format($er/$p, 6); 

// VALE5
$a[1][9]  = number_format($cg/$v, 6); 
$a[2][9]  = number_format($cu/$v, 6); 
$a[3][9]  = number_format($g/$v, 6);  
$a[4][9]  = number_format($s/$v, 6);  
$a[5][9]  = number_format($do/$v, 6); 
$a[6][9]  = number_format($b/$v, 6);  
$a[7][9]  = number_format($p/$v, 6);  
$a[8][9]  = 1;      
$a[9][9]  = number_format($o/$v, 6);  
$a[10][9] = number_format($u/$v, 6);  
$a[11][9] = number_format($dr/$v, 6); 
$a[12][9] = number_format($er/$v, 6); 

// OGXP3
$a[1][10] = number_format($cg/$o, 6); 
$a[2][10] = number_format($cu/$o, 6); 
$a[3][10] = number_format($g/$o, 6);  
$a[4][10] = number_format($s/$o, 6);  
$a[5][10] = number_format($do/$o, 6); 
$a[6][10] = number_format($b/$o, 6);  
$a[7][10] = number_format($p/$o, 6);  
$a[8][10] = number_format($v/$o, 6);  
$a[9][10] = 1;      
$a[10][10] = number_format($u/$o, 6); 
$a[11][10] = number_format($dr/$o, 6);
$a[12][10] = number_format($er/$o, 6);

// USIM5
$a[1][11] = number_format($cg/$u, 6);
$a[2][11] = number_format($cu/$u, 6);
$a[3][11] = number_format($g/$u, 6);
$a[4][11] = number_format($s/$u, 6);
$a[5][11] = number_format($do/$u, 6);
$a[6][11] = number_format($b/$u, 6);
$a[7][11] = number_format($p/$u, 6);
$a[8][11] = number_format($v/$u, 6);
$a[9][11] = number_format($o/$u, 6);
$a[10][11] = 1;
$a[11][11] = number_format($dr/$u, 6);
$a[12][11] = number_format($er/$u, 6);

// Dollar
$a[1][12] = number_format($cg/$dr, 6);
$a[2][12] = number_format($cu/$dr, 6);
$a[3][12] = number_format($g/$dr, 6);
$a[4][12] = number_format($s/$dr, 6);
$a[5][12] = number_format($do/$dr, 6);
$a[6][12] = number_format($b/$dr, 6);
$a[7][12] = number_format($p/$dr, 6);
$a[8][12] = number_format($v/$dr, 6);
$a[9][12] = number_format($o/$dr, 6);
$a[10][12] = number_format($u/$dr, 6);
$a[11][12] = 1;   
$a[12][12] = number_format($er/$dr, 6);;   

// Euro
$a[1][13] = number_format($cg/$er, 6);
$a[2][13] = number_format($cu/$er, 6);
$a[3][13] = number_format($g/$er, 6);
$a[4][13] = number_format($s/$er, 6);
$a[5][13] = number_format($do/$er, 6);
$a[6][13] = number_format($b/$er, 6);
$a[7][13] = number_format($p/$er, 6);
$a[8][13] = number_format($v/$er, 6);
$a[9][13] = number_format($o/$er, 6);
$a[10][13] = number_format($u/$er, 6);
$a[11][13] = number_format($dr/$er, 6);   
$a[12][13] = 1;   


// Convert number to month
$monthStr = array( 1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December");
echo "<h4>Assets in " . $monthStr[$month] . "/" . $year . "</h4>";

?> 


<!-- Tabela de ativos -->
<table border="1" width="60%">
<col width="160">
<col width="130">
<col width="130">
<col width="70">
<col width="60">
<col width="70">
<col width="70">
<col width="40">
<col width="40">
<col width="40">
<col width="40">
<col width="40">
<col width="40">
<col width="130">
<!--<caption>ASSETS IN JUN/2010</caption>-->
<?php
	echo "<tr>";
	echo "<th></th><th>Value</th><th>2Qtos C. Grande</th><th>2Qtos Curicica<th>Gold</th><th>Silver</th><th>Dow</th><th>Ibovespa</th><th>PETR4</th><th>VALE5</th><th>OGXP3</th><th>USIM5</th><th>Dollar</th><th>Euro</th>";
	echo "</tr>";

	echo "<tr>";
	echo "<th>2Qtos C. Grande</th><td align='center'>" . $a[1][1] . "</td><td bgcolor=#CCCCCC align='center'>" . $a[1][2] . "</td><td align='center'>" . $a[1][3] . "</td><td align='center'>" . $a[1][4] . "</td><td align='center'>" . $a[1][5] . "</td><td align='center'>" . $a[1][6] . "</td><td align='center'>" . $a[1][7] . "</td><td align='center'>" . $a[1][8] . "</td><td align='center'>" . $a[1][9] . "</td><td align='center'>" . $a[1][10] . "</td><td align='center'>" . $a[1][11] ."</td><td align='center'>" . $a[1][12] . "</td><td align='center'>" . $a[1][13] . "</td>";

	echo "</tr>";

	echo "<tr>";
	echo "<th>2Qtos Curicica</th><td align='center'>" . $a[2][1] . "</td><td align='center'>" . $a[2][2] . "</td><td bgcolor=#CCCCCC align='center'>" . $a[2][3] . "</td><td align='center'>" . $a[2][4] . "</td><td align='center'>" . $a[2][5] . "</td><td align='center'>" . $a[2][6] . "</td><td align='center'>" . $a[2][7] . "</td><td align='center'>" . $a[2][8] . "</td><td align='center'>" . $a[2][9] . "</td><td align='center'>" . $a[2][10] . "</td><td align='center'>" . $a[2][11] . "</td><td align='center'>" . $a[2][12] . "</td><td align='center'>" . $a[2][13] . "</td>";
	echo "</tr>";

	echo "<tr>";
	echo "<th>Gold</th><td align='center'>" . $a[3][1] . "</td><td align='center'>" . $a[3][2] . "</td><td>" . $a[3][3] . "</td><td bgcolor=#CCCCCC align='center'>" . $a[3][4] . "</td><td align='center'>" . $a[3][5] . "</td><td align='center'>" . $a[3][6] . "</td><td align='center'>" . $a[3][7] . "</td><td align='center'>" . $a[3][8] . "</td><td align='center'>" . $a[3][9] . "</td><td align='center'>" . $a[3][10] . "</td><td align='center'>" . $a[3][11] . "</td><td align='center'>" . $a[3][12] . "</td><td align='center'>" . $a[3][13] . "</td>";
	echo "</tr>";

	echo "<tr>";
	echo "<th>Silver</th><td align='center'>" . $a[4][1] . "</td><td align='center'>" . $a[4][2] . "</td><td align='center'>" . $a[4][3] . "</td><td align='center'>" . $a[4][4] . "</td><td bgcolor=#CCCCCC align='center'>" . $a[4][5] . "</td><td align='center'>" . $a[4][6] . "</td><td align='center'>" . $a[4][7] . "</td><td align='center'>" . $a[4][8] . "</td><td align='center'>" . $a[4][9] . "</td><td align='center'>" . $a[4][10] . "</td><td align='center'>" . $a[4][11] . "</td><td align='center'>" . $a[4][12] . "</td><td align='center'>" . $a[4][13] . "</td>";
	echo "</tr>";

	echo "<tr>";
	echo "<th>Dow</th><td align='center'>" . $a[5][1] . "</td><td align='center'>" . $a[5][2] . "</td><td align='center'>" . $a[5][3] . "</td><td align='center'>" . $a[5][4] . "</td><td align='center'>" . $a[5][5] . "</td><td bgcolor=#CCCCCC align='center'>" . $a[5][6] . "</td><td align='center'>" . $a[5][7] . "</td><td align='center'>" . $a[5][8] . "</td><td align='center'>" . $a[5][9] . "</td><td align='center'>" . $a[5][10] . "</td><td align='center'>" . $a[5][11] . "</td><td align='center'>" . $a[5][12] . "</td><td align='center'>" . $a[5][13] . "</td>";
	echo "</tr>";

	echo "<tr>";
	echo "<th>iBovespa</th><td align='center'>" . $a[6][1] . "</td><td align='center'>" . $a[6][2] . "</td><td align='center'>" . $a[6][3] . "</td><td align='center'>" . $a[6][4] . "</td><td align='center'>" . $a[6][5] . "</td><td align='center'>" . $a[6][6] . "</td><td bgcolor=#CCCCCC align='center'>" . $a[6][7] . "</td><td align='center'>" . $a[6][8] . "</td><td align='center'>" . $a[6][9] . "</td><td align='center'>" . $a[6][10] . "</td><td align='center'>" . $a[6][11] . "</td><td align='center'>" . $a[6][12] . "</td><td align='center'>" . $a[6][13] . "</td>";
	echo "</tr>";

	echo "<tr>";
	echo "<th>PETR4</th><td align='center'>" . $a[7][1] . "</td><td align='center'>" . $a[7][2] . "</td><td align='center'>" . $a[7][3] . "</td><td align='center'>" . $a[7][4] . "</td><td align='center'>" . $a[7][5] . "</td><td align='center'>" . $a[7][6] . "</td><td align='center'>" . $a[7][7] . "</td><td bgcolor=#CCCCCC align='center'>" . $a[7][8] . "</td><td align='center'>" . $a[7][9] . "</td><td align='center'>" . $a[7][10] . "</td><td align='center'>" . $a[7][11] . "</td><td align='center'>" . $a[7][12] . "</td><td align='center'>" . $a[7][13] . "</td>";
	echo "</tr>";

	echo "<tr>";
	echo "<th>VALE5</th><td align='center'>" . $a[8][1] . "</td><td align='center'>" . $a[8][2] . "</td><td align='center'>" . $a[8][3] . "</td><td align='center'>" . $a[8][4] . "</td><td align='center'>" . $a[8][5] . "</td><td align='center'>" . $a[8][6] . "</td><td align='center'>" . $a[8][7] . "</td><td align='center'>" . $a[8][8] . "</td><td bgcolor=#CCCCCC align='center'>" . $a[8][9] . "</td><td align='center'>" . $a[8][10] . "</td><td align='center'>" . $a[8][11] . "</td><td align='center'>" . $a[8][12] . "</td><td align='center'>" . $a[8][13] . "</td>";
	echo "</tr>";

	echo "<tr>";
	echo "<th>OGXP3</th><td align='center'>" . $a[9][1] . "</td><td align='center'>" . $a[9][2] . "</td><td align='center'>" . $a[9][3] . "</td><td align='center'>" . $a[9][4] . "</td><td align='center'>" . $a[9][5] . "</td><td align='center'>" . $a[9][6] . "</td><td align='center'>" . $a[9][7] . "</td><td align='center'>" . $a[9][8] . "</td><td align='center'>" . $a[9][9] . "</td><td bgcolor=#CCCCCC align='center'>" . $a[9][10] . "</td><td align='center'>" . $a[9][11] . "</td><td align='center'>" . $a[9][12] . "</td><td align='center'>" . $a[9][13] . "</td>";
	echo "</tr>";

	echo "<tr>";
	echo "<th>USIM5</th><td align='center'>" . $a[10][1] . "</td><td align='center'>" . $a[10][2] . "</td><td align='center'>" . $a[10][3] . "</td><td align='center'>" . $a[10][4] . "</td><td align='center'>" . $a[10][5] . "</td><td align='center'>" . $a[10][6] . "</td><td align='center'>" . $a[10][7] . "</td><td align='center'>" . $a[10][8] . "</td><td align='center'>" . $a[10][9] . "</td><td align='center'>" . $a[10][10] . "</td><td bgcolor=#CCCCCC align='center'>" . $a[10][11] . "</td><td align='center'>" . $a[10][12] . "</td><td align='center'>" . $a[10][13] . "</td>";
	echo "</tr>";

	echo "<tr>";
	echo "<th>Dollar</th><td align='center'>" . $a[11][1] . "</td><td align='center'>" . $a[11][2] . "</td><td align='center'>" . $a[11][3] . "</td><td align='center'>" . $a[11][4] . "</td><td align='center'>" . $a[11][5] . "</td><td align='center'>" . $a[11][6] . "</td><td align='center'>" . $a[11][7] . "</td><td align='center'>" . $a[11][8] . "</td><td align='center'>" . $a[11][9] . "</td><td align='center'>" . $a[11][10] . "</td><td align='center'>" . $a[11][11] . "</td><td bgcolor=#CCCCCC align='center'>" . $a[11][12] . "</td><td align='center'>" . $a[11][13] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<th>Euro</th><td align='center'>" . $a[12][1] . "</td><td align='center'>" . $a[12][2] . "</td><td align='center'>" . $a[12][3] . "</td><td align='center'>" . $a[12][4] . "</td><td align='center'>" . $a[12][5] . "</td><td align='center'>" . $a[12][6] . "</td><td align='center'>" . $a[12][7] . "</td><td align='center'>" . $a[12][8] . "</td><td align='center'>" . $a[12][9] . "</td><td align='center'>" . $a[12][10] . "</td><td align='center'>" . $a[12][11] . "</td><td align='center'>" . $a[12][12] . "</td><td  bgcolor=#CCCCCC align='center'>" . $a[12][13] . "</td>";
	echo "</tr>";
	echo "</table>";

	echo "<br>";

?> 

</body>
</html>
